//
//  MapViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/8/11.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "MapViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import "Masonry.h"
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>
#import <BaiduMapAPI_Search/BMKGeocodeSearchOption.h>
#import "MBProgressHUD.h"
@interface MapViewController ()<BMKLocationServiceDelegate,BMKMapViewDelegate,BMKGeoCodeSearchDelegate>
@property (nonatomic,strong) BMKMapView *mapView;
@property (nonatomic,strong) BMKLocationService *service;
@property (nonatomic,strong) BMKGeoCodeSearch *search;
@property (nonatomic,strong) BMKUserLocation *location;
@property (nonatomic,copy) NSString *mylocation;
@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setNewTitleCenter:@"位置"];
    self.mapView = [[BMKMapView alloc]init];
    self.mapView.delegate = self;
    self.mapView.zoomLevel = 18;
    [self.view addSubview:self.mapView];
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"backtobegin"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backToBegin) forControlEvents:UIControlEventTouchUpInside];
    [_mapView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(30);
    }];

    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:nil title:@"发送" target:self sel:@selector(senderImage) titleFont:Font18 titleColor:GreenColor];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
-(void)mapViewDidFinishLoading:(BMKMapView *)mapView
{
    if (mapView == _mapView) {
        [self configServer];
    }
}
-(void)configServer
{
    _service = [[BMKLocationService alloc]init];
    _service.delegate = self;
    [_service startUserLocationService];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.mapView animated:YES];
    hud.labelText = @"正在定位...";
    _search = [[BMKGeoCodeSearch alloc]init];
    _search.delegate = self;
    
}
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    [_service stopUserLocationService];
    _location = userLocation;
    BMKReverseGeoCodeOption *option = [BMKReverseGeoCodeOption new];
    option.reverseGeoPoint = userLocation.location.coordinate;
    [_search reverseGeoCode:option];

}

-(void)addAnnotation:(BMKUserLocation*)userlocation
{
     BMKPointAnnotation *annotation = [BMKPointAnnotation new];
     annotation.title =  self.mylocation;
     annotation.coordinate = userlocation.location.coordinate;
     [self.mapView addAnnotation:annotation];
}
-(void)senderImage
{
    if (self.MapManagerDelegate && [self.MapManagerDelegate respondsToSelector:@selector(MapViewControlleDidSendWithImage:lat:lon:)]) {
        [self.MapManagerDelegate MapViewControlleDidSendWithImage:[self getSnapshotImage] lat:_location.location.coordinate.latitude lon:_location.location.coordinate.longitude];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (UIImage *)getSnapshotImage {
    return [_mapView takeSnapshot];
}
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    self.mylocation = result.address;
    [self addAnnotation:_location];
    [MBProgressHUD hideHUDForView:_mapView];
    [self addinfomationView];
        self.navigationItem.rightBarButtonItem.enabled = YES;

}
-(void)addinfomationView
{
    UILabel *lable = [[UILabel alloc]init];
    lable.backgroundColor = [UIColor whiteColor];
    lable.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.numberOfLines = 0 ;
    lable.text = self.mylocation;
    [self.mapView addSubview:lable];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(80);
    }];
}
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorRed;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        //设置大头针在地图上是否可以拖动
        newAnnotationView.draggable = NO;
        [newAnnotationView setSelected:YES animated:YES];
        return newAnnotationView;
    }
    return nil;
}
-(void)backToBegin
{
    [_mapView setCenterCoordinate:_location.location.coordinate animated:YES];
}
-(void)dealloc
{
    
    _mapView.delegate = nil;
    _service.delegate = nil;
    _search.delegate = nil;
    if (_mapView) {
        _mapView = nil;
    }
}
@end


